package com.liming.util;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.liming.menu.Button;
import com.liming.menu.ClickButton;
import com.liming.menu.Menu;
import com.liming.menu.ViewButton;
import com.liming.po.AccessToken;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;

import net.sf.json.JSONObject;

public class WeixinUtil {
	// 微信公众平台测试号
	private static final String APPID = "wx00bd2be50bf128a8";
	private static final String APPSECRET = "90c7848322234bb681c063fa1a04b0f3";

	// 获取access_token 接口
	private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
	// 自定义创建菜单接口 post
	private static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";

	/**
	 * 发送 Get 请求
	 * 
	 * @param url
	 * @return JSONObject
	 */
	public static JSONObject doGetStr(String url) {
		JSONObject jsonObject = null; // json-lib
		// 创建默认的httpclient 实例
		CloseableHttpClient httpClient = HttpClients.createDefault();
		try {
			// 创建Get
			HttpGet httpGet = new HttpGet(url);
			// 发送Get
			CloseableHttpResponse response = httpClient.execute(httpGet);
			// 获取响应实体
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String result = EntityUtils.toString(entity, "UTF-8");
				// 转为JSON对象
				jsonObject = JSONObject.fromObject(result);
			}

		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接
			try {
				httpClient.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

		return jsonObject;
	}

	/**
	 * 发送 Post 请求
	 * 
	 * @param url
	 * @param outStr
	 * @return JSONObject
	 */
	public static JSONObject doPostStr(String url, String outStr) {
		JSONObject jsonObject = null;
		// 创建默认Httpclient实例
		CloseableHttpClient httpClient = HttpClients.createDefault();
		// 创建 Post
		HttpPost httpPost = new HttpPost(url);
		httpPost.setEntity(new StringEntity(outStr, "UTF-8"));

		try {
			CloseableHttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String result = EntityUtils.toString(entity);
				jsonObject = JSONObject.fromObject(result);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return jsonObject;
	}

	/**
	 * 获取 access_token
	 * 
	 * @return
	 */
	public static AccessToken getAccessToken() {
		AccessToken accessToken = new AccessToken();
		String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
		JSONObject jsonObject = doGetStr(url);

		if (jsonObject != null) {
			accessToken.setAccess_token(jsonObject.getString("access_token"));
			accessToken.setExpires_in(jsonObject.getString("expires_in"));
		}
		return accessToken;
	}

	/**
	 * 组装菜单
	 * 
	 * @return
	 */
	public static Menu initMenu() {
		Menu menu = new Menu();

		ClickButton button11 = new ClickButton();
		button11.setName("click菜单");
		button11.setType("click");
		button11.setKey("11");

		ViewButton button21 = new ViewButton();
		button21.setName("view跳转菜单");
		button21.setType("view");
		button21.setUrl("https://www.52liming.com");

		ClickButton button31 = new ClickButton();
		button31.setName("扫码事件");
		button31.setType("scancode_push");
		button31.setKey("31");

		ClickButton button32 = new ClickButton();
		button32.setName("地理位置");
		button32.setType("location_select");
		button32.setKey("32");

		Button button = new Button();
		button.setName("菜单");
		button.setSub_button(new Button[] { button31, button32 });

		menu.setButton(new Button[] { button11, button21, button });

		return menu;
	}

	// 创建菜单
	public static int createMenu(String token, String menu) {
		int result = 0;
		String url = CREATE_MENU_URL.replace("ACCESS_TOKEN", token);
		JSONObject jsonObject = doPostStr(url, menu);
		if (jsonObject != null) {
			result =jsonObject.getInt("errcode");
		}
		return result;
	}
}
